package test.functionQuery.hhpositionsort;

import org.apache.lucene.queries.function.ValueSource;
import org.apache.solr.common.SolrException;
import org.apache.solr.schema.SchemaField;
import org.apache.solr.schema.TrieDateField;
import org.apache.solr.search.FunctionQParser;
import org.apache.solr.search.SyntaxError;
import org.apache.solr.search.ValueSourceParser;

import com.hunteron.common4s.jdk.string.str.StringTools;
import com.hunteron.jdbc.dao.PositionHunterSortDao;
import com.hunteron.jdbc.dao.itf.IPositionHunterSortDao;

public class PositionSortSoruceParser extends ValueSourceParser {

    /*
     * //职位对应负责人HR的平均反馈天数 //暂时先用职位反馈时长 private int hrFeedbackDays = 100; //职位佣金 private double rewardAmount;
     * //职位发布时间距离当前时间的天数 private int publishDeltDays; //职位最后编辑时间距离当前时间的天数 public int modifyDeltDays; //职位是否调用 private
     * boolean isSurvey = false; //职位是否在运营有效期内 private boolean isOnOperation = false;(non-Javadoc)
     * @see org.apache.solr.search.ValueSourceParser#parse(org.apache.solr.search.FunctionQParser)
     */

    @Override
    public ValueSource parse(FunctionQParser fp) throws SyntaxError {

        long hunterId = StringTools.getLong(fp.parseArg(), -1);

        IPositionHunterSortDao hunterSortDao = new PositionHunterSortDao();
        // 职位编号
        ValueSource positionIdValueSource = getValueSource(fp, "position_id");
        // 职位默认排序值
        ValueSource defaultSortValueSource = getValueSource(fp, "d_long_position_default_sort_value");

        PositionSortValueSource tvs = new PositionSortValueSource(positionIdValueSource, defaultSortValueSource, hunterSortDao.getHunterSort(hunterId));

        return tvs;
    }

    public ValueSource getValueSource(FunctionQParser fp, String arg) {
        if (arg == null)
            return null;
        SchemaField f = fp.getReq().getSchema().getField(arg);
        if (f.getType().getClass() == TrieDateField.class) {
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Can't use ms() function on non-numeric legacy date field " + arg);
        }
        return f.getType().getValueSource(f, fp);
    }

}
